﻿using Newtonsoft.Json;
using AllInOne.Models;
using System;
using System.Collections.Generic;

namespace AllInOne.Classes
{
    internal class DataOperate
    {
        /// <summary>
        /// 获取称重数据
        /// </summary>
        /// <param name="authModel"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static WeighViewModel GetWeighInfo(AuthModel authModel, ref string message)
        {
            if (authModel != null && !string.IsNullOrWhiteSpace(authModel.AuthCode))
            {
                Log.Info($"打印磅单：AuthType={authModel.AuthType},AuthCode={authModel.AuthCode}");
                try
                {
                    WeighViewModel weighViewModel = new WeighViewModel();

                    CarWeightModel CarWeight = new CarWeightModel();
                    AppointmentModel Appointment = new AppointmentModel();
                    CarWeight.RecordId = "RcID001";
                    CarWeight.TareTime = Convert.ToDateTime("2022-03-04 10:24:35");
                    CarWeight.TareValue = 1000m;
                    CarWeight.GrossTime = Convert.ToDateTime("2022-03-04 08:09:39");
                    CarWeight.GrossValue = 20170m;
                    CarWeight.NetValue = 19170m;
                    CarWeight.WeightStatus = "1";

                    Appointment.UserName = "王利鹏";
                    Appointment.CarNumber = "豫A12345";
                    Appointment.GoodsTypeName = "物类型1";
                    Appointment.FromCompany = "发货单位1";
                    Appointment.ToCompany = "收货单位1";

                    weighViewModel.CarWeight = CarWeight;
                    weighViewModel.Appointment = Appointment;

                    return weighViewModel;
                }
                catch (Exception ex)
                {
                    message = "获取称重数据异常，请联系管理员";
                    Log.Error("PrintWeigh", ex);
                }
            }
            return null;
        }

        /// <summary>
        /// 测试--获取称重数据
        /// </summary>
        /// <param name="authModel"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static WeighViewModel GetWeighInfo_test(AuthModel authModel, ref string message)
        {
            if (authModel != null && !string.IsNullOrWhiteSpace(authModel.AuthCode))
            {
                Log.Info($"打印磅单：AuthType={authModel.AuthType},AuthCode={authModel.AuthCode}");
                try
                {
                    ResultModel<WeighViewModel> resultModel;
                    Dictionary<string, string> paras = new Dictionary<string, string>();
                    string resultString;
                    if (authModel.AuthType == 1)
                    {
                        paras.Add("identify_no", authModel.AuthCode);
                        //resultString = HttpHelper.PostHttpByKeyValue("/line/identNoQueryCarWeight", paras);
                        resultString = "";
                        resultModel = JsonConvert.DeserializeObject<ResultModel<WeighViewModel>>(resultString);
                    }
                    else
                    {
                        paras.Add("visit_code", authModel.AuthCode);
                        //resultString = HttpHelper.PostHttpByKeyValue("/line/scanQueryCarWeight", paras);
                        resultString = "";
                        resultModel = JsonConvert.DeserializeObject<ResultModel<WeighViewModel>>(resultString);
                    }
                    //Log.Info("磅单数据：" + JsonConvert.SerializeObject(resultModel));
                    if (resultModel.Code == 200)
                    {
                        return resultModel.Data;
                    }
                    else
                    {
                        Log.Info("称重数据接口返回值：" + JsonConvert.SerializeObject(resultModel));
                        message = "获取称重数据失败，请联系管理员";
                    }
                }
                catch (Exception ex)
                {
                    message = "获取称重数据异常，请联系管理员";
                    Log.Error("PrintWeigh", ex);
                }
            }
            return null;
        }
    }
}
